#include "stdafx.h"
#include <iostream>
#include "xglog.h"

#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib")
#include <windows.h>

#ifndef GLOG_NO_ABBREVIATED_SEVERITIES
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "glog\logging.h"
#endif

bool g_bTRA=false;


std::shared_ptr<xglog> xglog::p_this_ = nullptr;

xglog::xglog()
{
	pre_log_ = "";
}

xglog::~xglog()
{

}

std::string TCHAR2STRING(TCHAR *STR)
{

	int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, NULL);

	char* chRtn = new char[iLen * sizeof(char)];

	WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);

	std::string str(chRtn);

	return str;

}

std::wstring StringToWString(const std::string &str)
{
	std::wstring wstr(str.length(), L' ');
	std::copy(str.begin(), str.end(), wstr.begin());
	return wstr;
}

//void xglog::init(std::string log_path)
//{
//
//}
//
//void xglog::write_to_file(std::string path, std::string str, int color_plan) {
//
//}
//

void xglog::init(std::string log_path)
{
	google::InitGoogleLogging("");
	//google::InstallFailureWriter(&writer);
	FLAGS_logbufsecs = 0;	// Sets the maximum number of seconds that logs can be buffered, with 0 for real-time output
							// FLAGS_log_prefix = false;
							// Configure the Log path

	TCHAR szMbsFile[MAX_PATH] = { 0 };
	::GetModuleFileName(NULL, szMbsFile, MAX_PATH);
	std::string tmp = TCHAR2STRING(szMbsFile);

	/*std::ostringstream ss;
	ss << tmp.substr(0, tmp.rfind("\\"));
	ss << "\\";
	ss << log_path;
	ss << "\\";

	if (!PathIsDirectory(StringToWString(ss.str()).c_str()))
	{
	::CreateDirectory(StringToWString(ss.str()).c_str(), NULL);
	}*/
	std::string tt = x_global::get_current_path() + "log/";
	google::SetLogDestination(google::GLOG_INFO, tt.c_str());

}

void xglog::write_to_file(std::string path, std::string str, int color_plan)
{
	std::string tmp = path + ":\t\t" + str + "\t\t";
	google::LogMessage(tmp.c_str(), __LINE__);
}